<template>
  <div>
<el-alert :title="`数据一共${counts}条`" type="info" show-icon />
<!-- 表格 -->
 <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column
      prop="number"
      label="试题编号"
      width="150">
    </el-table-column>
    <el-table-column
      prop="subject"
      label="学科"
      width="120">
    </el-table-column>
    <el-table-column
      prop="catalog"
      label="目录"
      width="120">
    </el-table-column>
    <el-table-column
      prop="questionType"
      label="题型"
      width="120">
      <template slot-scope="{row}">
        {{questionType(row.questionType)}}
      </template>
    </el-table-column>
    <el-table-column
      label="题干"
      width="260">
      <template slot-scope="{row}">
        <div v-html="row.question" />
      </template>
    </el-table-column>
    <el-table-column
      label="录入时间"
      width="180">
      <template slot-scope="{row}">
        {{row.addDate | parseTimeByString}}
      </template>
    </el-table-column>
    <el-table-column
      label="难度"
      width="80">
      <template slot-scope="{row}">
        {{difficulty(row.difficulty)}}
      </template>
    </el-table-column>
    <el-table-column
      prop="creator"
      label="录入人"
      width="120">
    </el-table-column>
    <el-table-column
      prop="chkState"
      label="审核状态"
      width="120">
      <template slot-scope="{row}">
        {{chkType(row.chkState)}}
      </template>
    </el-table-column>
    <el-table-column
      prop="chkRemarks"
      label="审核意见"
      width="120">
    </el-table-column>
     <el-table-column
      prop="chkUser"
      label="审核人"
      width="120">
    </el-table-column>
     <el-table-column
      prop="creatorID"
      label="发布状态"
      width="120">
      <template slot-scope="{row}">
        {{publishType(row.creatorID)}}
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="200">
      <template slot-scope="{row}">
        <el-button @click="handleClick(row)" type="text" :disabled="isDisable('table-list-list')" size="small">预览</el-button>
        <el-button type="text" size="small" @click="clickExamine(row)" :disabled="row.chkState === 0 ? false:true">审核</el-button>
        <el-button type="text" size="small" @click="$router.push({path:'new',query:{id:row.id}})">修改</el-button>
        <el-button type="text" size="small" @click="showPublishState(row)">{{row.publishState===1?'上架':'下架'}}</el-button>
        <el-button type="text" size="small" @click="Delete(row)" :disabled="row.publishState === 1 ? false:true">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <Pagination :counts='counts' @changePageSize='changePageSize' @changeCurrentPage='changeCurrentPage' />

  <!-- 预览框 -->
  <Descriptions :DescInfo='DescInfo' :isShowDesc='isShowDesc' @onClose='isShowDesc=$event' />

  <!-- 审核对话框 -->
  <examine-dialog @onSuccess='onSuccess' :examineInfo='examineInfo' @onClose='isShowExamine=$event' :isShowExamine='isShowExamine' />
  </div>
</template>

<script>
import { choice, detail, choicePublish, remove } from '@/api/hmmm/questions.js'
import { difficulty, chkType, publishType, questionType } from '@/api/hmmm/constants.js'
import { createNamespacedHelpers } from 'vuex'
import examineDialog from './examine-dialog.vue'
const { mapState } = createNamespacedHelpers('user')
export default {
  components: { examineDialog },
  name: 'AllQuestion',
  computed: {
    ...mapState(['points']),
    questionType (type) {
      return type => questionType.find(item => item.value === type - 0).label
    },
    difficulty (diff) {
      return diff => difficulty.find(item => item.value === diff - 0).label
    },
    chkType (type) {
      return type => chkType.find(item => item.value === type - 0 + 1).label
    },
    publishType (type) {
      return type => publishType.find(item => item.value === type - 0).label
    }
  },
  props: {
    pageForm: {
      type: Object,
      required: true
    } // 分页数据
  },

  data () {
    return {
      isShowDesc: false, // 预览框是否显示
      isShowExamine: false, // 审核框是否显示
      tableData: [], // 表格渲染数据
      counts: 0, // 总条数
      DescInfo: {}, // 预览所需数据
      examineInfo: {}// 审核所需数据
    }
  },

  created () {
    this.loadTableInfo()
  },

  methods: {
    // 按钮是否禁用
    isDisable (val) {
      const point = this.$store.state.user.roles.points
      return !point.includes(val)
    },
    // 加载表格数据
    async loadTableInfo (e) {
      const form = { ...e, ...this.pageForm } || this.pageForm
      const res = await choice(form)
      this.tableData = res.data.items
      console.log(res.data.items)
      this.counts = res.data.counts
    },
    // 预览按钮点击事件
    async handleClick (row) {
      const res = await detail(row)
      this.isShowDesc = true
      this.DescInfo = res.data
      console.log(this.DescInfo)
    },
    // 显示数量变化
    changePageSize (e) {
      this.pageForm.pagesize = e
      this.loadTableInfo()
    },
    // 当前页码发生改变
    changeCurrentPage (e) {
      this.pageForm.page = e
      this.loadTableInfo()
    },
    // 审核按钮点击事件
    clickExamine (e) {
      this.isShowExamine = true
      this.examineInfo = e
    },
    // 审核对话框保存
    onSuccess () {
      this.isShowExamine = false
      this.loadTableInfo()
    },
    // 上下架消息提示
    showPublishState (row) {
      this.$confirm(`您确认${row.publishState ? '上架' : '下架'}这道题目吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        row.publishState = row.publishState === 1 ? 0 : 1
        await choicePublish(row)
        this.$message({
          type: 'success',
          message: `${!row.publishState ? '上架' : '下架'}成功!`
        })
        this.loadTableInfo()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    },
    // 删除
    Delete (row) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadTableInfo()
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-alert{
  margin-bottom: 15px;
}
</style>
